﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Company.Shell.Controls
{
    /// <summary>
    /// ACU.xaml 的交互逻辑
    /// </summary>
    public partial class ACU : UserControl
    {
        private DoubleAnimation intakeAnimation;
        private DoubleAnimation exhaustAnimation;
        private DoubleAnimation condenserAnimation;


        public ACU()
        {
            InitializeComponent();
            InitializeAnimations();
        }

        /// <summary>
        /// ACU运行状态：0=停止，1=运行，2=故障
        /// </summary>
        public int State
        {
            get { return (int)GetValue(StateProperty); }
            set { SetValue(StateProperty, value); }
        }

        public static readonly DependencyProperty StateProperty =
            DependencyProperty.Register("State", typeof(int), typeof(ACU),
                new PropertyMetadata(0, new PropertyChangedCallback(StateChangedCallback)));

        private static void StateChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (ACU)d;
            if (control == null) return;

            if (e.NewValue is int state)
            {
                control.UpdateACUState(state);
            }
        }

        /// <summary>
        /// 温度值
        /// </summary>
        public double Temperature
        {
            get { return (double)GetValue(TemperatureProperty); }
            set { SetValue(TemperatureProperty, value); }
        }

        public static readonly DependencyProperty TemperatureProperty =
            DependencyProperty.Register("Temperature", typeof(double), typeof(ACU),
                new PropertyMetadata(25.0, new PropertyChangedCallback(TemperatureChangedCallback)));

        private static void TemperatureChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (ACU)d;
            if (control == null) return;

            if (e.NewValue is double temperature)
            {
                control._temperatureText.Text = $"{temperature:F1}°C";
            }
        }

        /// <summary>
        /// 功率百分比
        /// </summary>
        public int Power
        {
            get { return (int)GetValue(PowerProperty); }
            set { SetValue(PowerProperty, value); }
        }

        public static readonly DependencyProperty PowerProperty =
            DependencyProperty.Register("Power", typeof(int), typeof(ACU),
                new PropertyMetadata(0, new PropertyChangedCallback(PowerChangedCallback)));

        private static void PowerChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (ACU)d;
            if (control == null) return;

            if (e.NewValue is int power)
            {
                control._powerText.Text = $"{power}%";
            }
        }

        private void UpdateACUState(int state)
        {
            switch (state)
            {
                case 0: // 停止状态
                    _runIndicator.Background = new SolidColorBrush(Color.FromRgb(128, 128, 128));
                    _faultIndicator.Background = new SolidColorBrush(Color.FromRgb(128, 128, 128));
                    _statusText.Text = "停止";
                    _statusText.Foreground = new SolidColorBrush(Color.FromRgb(255, 64, 64));
                    _condenserIndicator.Fill = new SolidColorBrush(Color.FromRgb(128, 128, 128));
                    StopAnimations();
                    break;

                case 1: // 运行状态
                    _runIndicator.Background = new SolidColorBrush(Color.FromRgb(64, 255, 64));
                    _faultIndicator.Background = new SolidColorBrush(Color.FromRgb(128, 128, 128));
                    _statusText.Text = "运行";
                    _statusText.Foreground = new SolidColorBrush(Color.FromRgb(64, 255, 64));
                    _condenserIndicator.Fill = new SolidColorBrush(Color.FromRgb(64, 128, 255));
                    StartAnimations();
                    break;

                case 2: // 故障状态
                    _runIndicator.Background = new SolidColorBrush(Color.FromRgb(128, 128, 128));
                    _faultIndicator.Background = new SolidColorBrush(Color.FromRgb(255, 64, 64));
                    _statusText.Text = "故障";
                    _statusText.Foreground = new SolidColorBrush(Color.FromRgb(255, 64, 64));
                    _condenserIndicator.Fill = new SolidColorBrush(Color.FromRgb(255, 64, 64));
                    StopAnimations();
                    break;
            }
        }

        private void InitializeAnimations()
        {
            // 进风动画
            intakeAnimation = new DoubleAnimation()
            {
                From = 0.3,
                To = 1.0,
                Duration = TimeSpan.FromSeconds(1.0),
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever
            };

            // 出风动画
            exhaustAnimation = new DoubleAnimation()
            {
                From = 0.3,
                To = 1.0,
                Duration = TimeSpan.FromSeconds(0.8),
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever
            };

            // 冷凝器工作指示动画
            condenserAnimation = new DoubleAnimation()
            {
                From = 0.5,
                To = 1.0,
                Duration = TimeSpan.FromSeconds(2.0),
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever
            };
        }

        private void StartAnimations()
        {
            _intakeArrow.BeginAnimation(OpacityProperty, intakeAnimation);
            _exhaustArrow.BeginAnimation(OpacityProperty, exhaustAnimation);
            _condenserIndicator.BeginAnimation(OpacityProperty, condenserAnimation);
        }

        private void StopAnimations()
        {
            _intakeArrow.BeginAnimation(OpacityProperty, null);
            _exhaustArrow.BeginAnimation(OpacityProperty, null);
            _condenserIndicator.BeginAnimation(OpacityProperty, null);
            
            _intakeArrow.Opacity = 0;
            _exhaustArrow.Opacity = 0;
            _condenserIndicator.Opacity = 1;
        }
    }
}
